package com.shanghaionstar.navi;

import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.FragmentManager;
import android.text.Editable;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.amap.api.services.help.Inputtips;
import com.amap.api.services.help.Tip;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.framework.utils.RxBus;
import com.onstar.cn.map.common.LocationType;
import com.onstar.cn.map.common.PoiItemInfo;
import com.onstar.cn.weight.DeleteEditText;
import com.shanghaionstar.R;
import com.shanghaionstar.nav.service.AbstractMapActivity;
import com.shanghaionstar.navi.search.history.NavSearchHistoryFragment;
import com.shanghaionstar.navi.search.result.NavSearchResultFragment;
import com.shanghaionstar.utils.MapHandler;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by zhaoyang on 2017/7/20.
 */

public class NavSearchActivity extends AbstractMapActivity {

    @BindView(R.id.nav_search_back_iv)
    ImageView mBackIv;

    @BindView(R.id.nav_search_et)
    DeleteEditText mSearchEt;

    @BindView(R.id.nav_search_delete_iv)
    ImageView mDeleteIv;

    @BindView(R.id.nav_search_delete_ly)
    FrameLayout mDeleteLy;

    @BindView(R.id.nav_search_search_tv)
    TextView mSearchTv;

    @BindView(R.id.nav_search_back_ly)
    FrameLayout mBackLy;

    @BindView(R.id.nav_search_top_ly)
    LinearLayout mSearchLy;

    @BindView(R.id.nav_search_fragment_container)
    FrameLayout mFragContainer;

    private MapHandler mMapHandler;
    private FragmentManager mFragManager;
    private NavSearchHistoryFragment mHistoryFragment;
    private NavSearchResultFragment mResultFragment;

    @Override
    public BasePresenter getPresenter() {
        return null;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_nav_search);
        ButterKnife.bind(this);

        initEditText();
        mFragManager = getSupportFragmentManager();

        mHistoryFragment = new NavSearchHistoryFragment();
        mResultFragment = new NavSearchResultFragment();

        mFragManager.beginTransaction().add(R.id.nav_search_fragment_container, mHistoryFragment).commit();
        mFragManager.beginTransaction().add(R.id.nav_search_fragment_container, mResultFragment).commit();

        mFragManager.beginTransaction().show(mHistoryFragment).commit();
        mFragManager.beginTransaction().hide(mResultFragment).commit();
    }

    @Override
    public void initMapHandler(MapHandler mapHandler) {
        mMapHandler = mapHandler;
    }

    @Override
    public void showPoiToMap(PoiItemInfo poiItemInfo, LocationType locationType) {

    }

    private void initEditText() {
        // 原生删除图标丑
        mSearchEt.setShowClearBtn(false);
        mSearchEt.setOnTextChangeListener(new DeleteEditText.OnTextChangeListener() {
            @Override
            public void textChangeListener(View view, CharSequence text) {
                boolean isEmpty = CommonUtil.isNull(text.toString());

                mDeleteLy.setVisibility(isEmpty ? View.INVISIBLE : View.VISIBLE);
                mDeleteLy.setClickable(!isEmpty);
                mDeleteLy.setEnabled(!isEmpty);
            }

            @Override
            public void afterTextChanged(Editable s) {
                boolean isEmpty = CommonUtil.isNull(mSearchEt.getText().toString().trim());

                switchFragment(isEmpty);
                fetchInputTips(mSearchEt.getText().toString());

                if (isEmpty)
                    mSearchEt.clearFocus();
            }
        });
    }

    private void fetchInputTips(String inputText) {
        Inputtips inputTips = new Inputtips(NavSearchActivity.this.getApplicationContext(),
                new Inputtips.InputtipsListener() {

                    @Override
                    public void onGetInputtips(List<Tip> tipList, int rCode) {
                        System.out.println(rCode + "");
                        System.out.println(tipList == null ? "null" : tipList.size());
                        if (rCode == 0 && (tipList != null && tipList.size() > 0)) {// 正确返回
                            Bundle bundle = new Bundle();
                            bundle.putParcelableArrayList("tips_list", new ArrayList<Parcelable>(tipList));
                            RxBus.getDefault().post(888, bundle);
                        }else {
                            RxBus.getDefault().post(999, "无搜索结果。");
                        }
                    }
                });

        try {
            inputTips.requestInputtips(inputText, "SH");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void switchFragment(boolean isEmpty) {
        mFragManager.beginTransaction().hide(isEmpty ? mResultFragment : mHistoryFragment).commit();
        mFragManager.beginTransaction().show(isEmpty ? mHistoryFragment : mResultFragment).commit();
    }

    @OnClick({R.id.nav_search_back_ly, R.id.nav_search_delete_ly})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.nav_search_back_ly:
                finish();
                break;

            case R.id.nav_search_delete_ly:
                mSearchEt.clearAll();
                break;
        }
    }
}
